package com.example.demo.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Base64;
import org.json.JSONObject;
public class c {

    static String biao_user = "user";
    static String biao_pinglun = "pinglun";
    static String biao_shoucang = "shoucang";
    static String biao_dianzan = "dianzan";
    static String biao_chat = "chat";

    public static String zhixing(String method) {
        String res = "";
        if (method.equals("info_charu")) {
            res = info_charu();
        }
        if (method.equals("info_xiugai")) {
            res = info_xiugai();
        }
        if (method.equals("common_delete")) {
            res = common_delete();
        }
        if (method.equals("common_delete_status")) {
            res = common_delete_status();
        }
        if (method.equals("common_select")) {
            res = common_select();
        }
        if (method.equals("common_list")) {
            res = common_list();
        }
        if (method.equals("common_list_status")) {
            res = common_list_status();
        }
        if (method.equals("common_detail")) {
            res = common_detail();
        }
        if (method.equals("user_login_u")) {
            res = user_login_u();
        }
        if (method.equals("user_wxlogin")) {
            res = user_wxlogin();
        }
        if (method.equals("user_login_p")) {
            res = user_login_p();
        }
        if (method.equals("user_onlogin")) {
            res = user_onlogin();
        }
        if (method.equals("user_onres")) {
            res = user_onres();
        }
        if (method.equals("user_xiugai")) {
            res = user_xiugai();
        }
        if (method.equals("user_detail")) {
            res = user_detail();
        }
        if (method.equals("pinglun_list")) {
            res = pinglun_list();
        }
        if (method.equals("shoucang")) {
            res = shoucang();
        }
        if (method.equals("dianzan")) {
            res = dianzan();
        }
        if (method.equals("luntan_getlist")) {
            res = luntan_getlist();
        }
        if (method.equals("luntan_getlist_mine")) {
            res = luntan_getlist_mine();
        }
        if (method.equals("luntan_getlist_myshoucang")) {
            res = luntan_getlist_myshoucang();
        }
        if (method.equals("luntan_detail")) {
            res = luntan_detail();
        }
        if (method.equals("sousuo")) {
            res = sousuo();
        }
        if (method.equals("sousuo_status")) {
            res = sousuo_status();
        }
        if (method.equals("uptu")) {
            res = uptu();
        }
        if (method.equals("kefu_getuser")) {
            res = kefu_getuser();
        }
        if (method.equals("kefu_getlist")) {
            res = kefu_getlist();
        }
        if (method.equals("kefu_send")) {
            res = kefu_send();
        }
        if (method.equals("kefu_sendimg")) {
            res = kefu_sendimg();
        }
        if (method.equals("kefu_skip")) {
            res = kefu_skip();
        }
        if (method.equals("xiaoshou_register")) {
            res = xiaoshou_register();
        }
        if (method.equals("caigou_register")) {
            res = caigou_register();
        }

        if (method.equals("chat_send")) {
            res = chat_send();
        }

        if (method.equals("chat_getnew")) {
            res = chat_getnew();
        }

        if (method.equals("chat_getname")) {
            res = chat_getname();
        }

        return res;
    }

    static String user_login_u() {
        String username = manifest.jsonObject.getString("username");
        String password = manifest.jsonObject.getString("password");
        int num = manifest.chaxun_num("SELECT * FROM " + biao_user + " where username = '" + username
                + "' and password = '" + password + "'");
        String res = "";
        if (num == 0) {
            manifest.charu(biao_user, "username,password");
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where username = '" + username + "'");
        } else {
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where username = '" + username + "'");
        }
        return res;
    }

    static String user_login_p() {
        String phone = manifest.jsonObject.getString("phone");
        String password = manifest.jsonObject.getString("password");
        int num = manifest.chaxun_num("SELECT * FROM " + biao_user + " where phone = '" + phone
                + "' and password = '" + password + "'");
        String res = "";
        if (num == 0) {
            manifest.charu(biao_user, "phone,password");
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where phone = '" + phone + "'");
        } else {
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where phone = '" + phone + "'");
        }
        return res;
    }

    static String user_onlogin() {
        String username = manifest.jsonObject.getString("username");
        String password = manifest.jsonObject.getString("password");
        String res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where username = '" + username
                + "' and password = '" + password + "'");
        return res;
    }

    static String user_onres() {
        String username = manifest.jsonObject.getString("username");
        int num = manifest.chaxun_num("SELECT * FROM " + biao_user + " where username = '" + username + "'");
        if (num == 0) {
            manifest.charu(biao_user, "username,password,name,phone,dizhi");
            return manifest.chaxun_one("SELECT * FROM " + biao_user + " where username = '" + username + "'");
        } else {
            JSONObject j = new JSONObject();
            j.put("code", 300);
            return j.toString();
        }
    }

    static String xiaoshou_register() {
        String username = manifest.jsonObject.getString("username");
        int num = manifest.chaxun_num("SELECT * FROM xiaoshou where username = '" + username + "'");
        if (num == 0) {
            manifest.charu("xiaoshou", "username,password,name,phone,head");
            return manifest.chaxun_one("SELECT * FROM xiaoshou where username = '" + username + "'");
        } else {
            JSONObject j = new JSONObject();
            j.put("code", 300);
            return j.toString();
        }
    }

    static String caigou_register() {
        String username = manifest.jsonObject.getString("username");
        int num = manifest.chaxun_num("SELECT * FROM caigou where username = '" + username + "'");
        if (num == 0) {
            manifest.charu("caigou", "username,password,name,phone,head");
            return manifest.chaxun_one("SELECT * FROM caigou where username = '" + username + "'");
        } else {
            JSONObject j = new JSONObject();
            j.put("code", 300);
            return j.toString();
        }
    }

    static String user_wxlogin() {
        String wxid = manifest.jsonObject.getString("wxid");
        int num = manifest.chaxun_num("SELECT * FROM " + biao_user + " where wxid = '" + wxid + "'");
        String res = "";
        if (num == 0) {
            manifest.charu(biao_user, "wxid,wxhead,wxname");
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where wxid = '" + wxid + "'");
        } else {
            res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where wxid = '" + wxid + "'");
        }
        return res;
    }

    static String user_detail() {
        String id = manifest.jsonObject.opt("id").toString();
        String res = manifest.chaxun_one("SELECT * FROM " + biao_user + " where id = " + id);
        return res;
    }

    static String user_xiugai() {
        String arr = manifest.arr;
        String res = manifest.xiugai(biao_user, arr, null);
        return res;
    }

    static String info_charu() {
        String arr = manifest.arr;
        String res = manifest.charu(manifest.biao, arr);
        return res;
    }

    static String info_xiugai() {
        String arr = manifest.arr;
        String res = manifest.xiugai(manifest.biao, arr, null);
        return res;
    }

    static String common_delete() {
        int id = manifest.jsonObject.getInt("id");
        String res = manifest.shanchu(manifest.biao, id);
        return res;
    }

    static String common_delete_status() {
        int id = manifest.jsonObject.getInt("id");
        String biao = manifest.biao;
        String sql = "UPDATE " + biao + " set status = 1 where id = " + id;
        String res = manifest.caozuo(sql);
        return res;
    }

    static String common_select() {
        String sql = manifest.jsonObject.getString("sql");
        return manifest.chaxun(sql);
    }

    static String common_list() {
        String biao = manifest.biao;
        String limit = manifest.jsonObject.has("limit") ? manifest.jsonObject.opt("limit").toString() : null;
        String order = manifest.jsonObject.has("order") ? manifest.jsonObject.getString("order") : null;
        String tj = manifest.jsonObject.has("tj") ? manifest.jsonObject.getString("tj") : null;

        String sql = "";
        String tjsql = "";
        String ordersql = "";
        String limitsql = "";

        // 如果存在条件，那么先对条件进行处理
        List<String> arr;
        if (tj != null) {
            arr = Arrays.asList(tj.split(","));
            for (int i = 0; i < arr.size(); i++) {
                String s = arr.get(i) + " = '" + manifest.jsonObject.get(arr.get(i)) + "'";
                tjsql = tjsql == "" ? (" where " + s) : (tjsql + "  and " + s);
            }
        }
        if (order != null) {
            ordersql = " order by " + manifest.jsonObject.getString("order") + " desc ";
        }
        if (limit != null) {
            limitsql = " limit " + limit;
        }

        sql = "SELECT * FROM " + biao + tjsql + ordersql + limitsql;
        String res = manifest.chaxun(sql);
        return res;
    }

    static String common_list_status() {
        String biao = manifest.biao;
        String limit = manifest.jsonObject.has("limit") ? manifest.jsonObject.opt("limit").toString() : null;
        String order = manifest.jsonObject.has("order") ? manifest.jsonObject.getString("order") : null;
        String tj = manifest.jsonObject.has("tj") ? manifest.jsonObject.getString("tj") : null;

        String sql = "";
        String tjsql = "";
        String ordersql = "";
        String limitsql = "";

        // 如果存在条件，那么先对条件进行处理
        List<String> arr;
        if (tj != null) {
            arr = Arrays.asList(tj.split(","));
            for (int i = 0; i < arr.size(); i++) {
                String s = arr.get(i) + " = '" + manifest.jsonObject.get(arr.get(i)) + "'";
                tjsql = tjsql == "" ? (" where " + s) : (tjsql + "  and " + s);
            }
        }
        tjsql = tjsql == "" ? (" where status = 0 ") : (tjsql + " and status = 0 ");
        if (order != null) {
            ordersql = " order by " + manifest.jsonObject.getString("order") + " desc ";
        }
        if (limit != null) {
            limitsql = " limit " + limit;
        }

        sql = "SELECT * FROM " + biao + tjsql + ordersql + limitsql;
        String res = manifest.chaxun(sql);
        return res;
    }

    static String common_detail() {
        String biao = manifest.biao;
        String limit = "1";
        String order = manifest.jsonObject.has("order") ? manifest.jsonObject.getString("order") : null;
        String tj = manifest.jsonObject.has("tj") ? manifest.jsonObject.getString("tj") : "id";

        String sql = "";
        String tjsql = "";
        String ordersql = "";
        String limitsql = "";

        // 如果存在条件，那么先对条件进行处理
        List<String> arr;
        if (tj != null) {
            arr = Arrays.asList(tj.split(","));
            for (int i = 0; i < arr.size(); i++) {
                String s = arr.get(i) + " = '" + manifest.jsonObject.opt(arr.get(i)).toString() + "'";
                tjsql = tjsql == "" ? (" where " + s) : (tjsql + "  and " + s);
            }
        }
        if (order != null) {
            ordersql = " order by " + manifest.jsonObject.getString("order") + " desc ";
        }
        if (limit != null) {
            limitsql = " limit " + limit;
        }

        sql = "SELECT * FROM " + biao + tjsql + ordersql + limitsql;
        String res = manifest.chaxun_one(sql);
        return res;
    }

    static String pinglun_list() {
        String key = manifest.jsonObject.getString("key");
        String biao = manifest.jsonObject.has("biao") ? manifest.jsonObject.getString("biao") : biao_pinglun;
        String sql = String.format("""
                SELECT u.*,p.* FROM %s p,%s u where p.%s = '%s' and p.uid = u.id order by p.id desc
                """, biao, biao_user, key, manifest.jsonObject.opt(key).toString());
        System.out.println(sql);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String shoucang() {
        String res = manifest.youwu(biao_shoucang, manifest.jsonObject.getString("key"));
        return res;
    }

    static String dianzan() {
        String res = manifest.youwu(biao_dianzan, manifest.jsonObject.getString("key"));
        return res;
    }

    static String luntan_getlist() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        String sql = String.format("""
                SELECT u.wxhead,u.wxname,l.*,(SELECT count(*) FROM %s where lid = l.id) as dianzan,
                (SELECT count(*) FROM %s where lid = l.id) as pinglun,
                (SELECT count(*) FROM %s where lid = l.id) as shoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mydianzan,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as myshoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mypinglun
                FROM luntan l,user u where l.uid = u.id order by l.id desc
                    """, biao_dianzan, biao_pinglun, biao_shoucang, biao_dianzan,
                uid, biao_shoucang, uid, biao_pinglun, uid);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String luntan_getlist_mine() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        String sql = String.format("""
                SELECT u.wxhead,u.wxname,l.*,(SELECT count(*) FROM %s where lid = l.id) as
                dianzan,
                (SELECT count(*) FROM %s where lid = l.id) as pinglun,
                (SELECT count(*) FROM %s where lid = l.id) as shoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mydianzan,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as myshoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mypinglun
                FROM luntan l,user u where l.uid = %d and u.id = %d order by l.id desc
                """, biao_dianzan, biao_pinglun, biao_shoucang, biao_dianzan, uid,
                biao_shoucang,
                uid, biao_pinglun, uid, uid, uid);
        // String sql = """
        // SELECT u.wxhead,u.wxname,l.* FROM luntan l,user u where l.uid = %d and u.id =
        // %d order by l.id desc
        // """.formatted(uid, uid);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String luntan_getlist_myshoucang() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        String sql = String.format("""
                SELECT u.wxhead,u.wxname,l.*,(SELECT count(*) FROM %s where lid = l.id) as dianzan,
                (SELECT count(*) FROM %s where lid = l.id) as pinglun,
                (SELECT count(*) FROM %s where lid = l.id) as shoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mydianzan,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as myshoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mypinglun
                FROM luntan l,user u where l.uid = u.id and
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) >0
                order by l.id desc
                    """, biao_dianzan, biao_pinglun, biao_shoucang, biao_dianzan, uid, biao_shoucang,
                uid, biao_pinglun, uid, biao_shoucang, uid);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String luntan_detail() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        int lid = Integer.valueOf(manifest.jsonObject.opt("id").toString());
        String sql = String.format("""
                SELECT u.wxhead,u.wxname,l.*,
                (SELECT count(*) FROM %s where lid = l.id) as dianzan,
                (SELECT count(*) FROM %s where lid = l.id) as pinglun,
                (SELECT count(*) FROM %s where lid = l.id) as shoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mydianzan,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as myshoucang,
                (SELECT count(*) FROM %s where lid = l.id and uid = %d) as mypinglun
                 FROM luntan l,user u where l.uid = u.id and l.id = %d
                """, biao_dianzan, biao_pinglun, biao_shoucang, biao_dianzan, uid, biao_shoucang, uid,
                biao_pinglun, uid, lid);
        String res = manifest.chaxun_one(sql);
        return res;
    }

    static String sousuo() {
        String biao = manifest.biao;
        String key = manifest.jsonObject.getString("key");
        String zhi = manifest.jsonObject.getString("value");
        String sql = String.format("""
                SELECT * FROM %s where %s like '%%s%'
                """, biao, key, zhi);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String sousuo_status() {
        String biao = manifest.biao;
        String key = manifest.jsonObject.getString("key");
        String zhi = manifest.jsonObject.getString("value");
        String sql = String.format("""
                SELECT * FROM %s where %s like '%%s%' and status = 0
                """, biao, key, zhi);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String uptu() {
        JSONObject j = new JSONObject();
        String base64 = manifest.jsonObject.getString("base64");
        String name = manifest.jsonObject.opt("name").toString();
        String imageName = name + gongju.suiji(11, 99) + ".png";

        if (base64.contains(",")) {
            String[] parts = base64.split(",");
            base64 = parts[1];
        }

        try {
            byte[] imageBytes = Base64.getDecoder().decode(base64);
            // 保存图片到指定目录
            FileOutputStream outputStream = new FileOutputStream("files/" + imageName);
            outputStream.write(imageBytes);
            outputStream.flush();
            outputStream.close();

            j.put("code", 200);
            j.put("data", imageName);
            return j.toString();
        } catch (IOException e) {
            System.out.println("保存图片时出错：" + e.getMessage());
            j.put("code", 300);
            return j.toString();
        }
    }

    static String kefu_getuser() {
        String sql = "SELECT c.uid,c.addtime,x.wxhead, x.wxname FROM (SELECT uid,addtime FROM kefu order by id desc ) as c,user x where x.id = c.uid group by uid";
        String res = manifest.chaxun(sql);
        return res;
    }

    static String kefu_getlist() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        String sql = String.format("""
                SELECT k.*,u.wxhead,u.wxname FROM kefu k,user u where k.uid = u.id and u.id = %d
                     """, uid);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String kefu_send() {
        String res = manifest.charu("kefu", "neirong,uid,who");
        return res;
    }

    static String kefu_sendimg() {
        String res = manifest.charu("kefu", "neirong,uid,who,img");
        return res;
    }

    static String kefu_skip() {
        int uid = Integer.valueOf(manifest.jsonObject.opt("uid").toString());
        int skip = Integer.valueOf(manifest.jsonObject.opt("skip").toString());
        String sql = String.format("""
                SELECT k.*,u.wxhead,u.wxname FROM kefu k,user u where k.uid = u.id and u.id = %d and k.id > %d
                    """, uid, skip);
        String res = manifest.chaxun(sql);
        return res;
    }

    static String chat_send() {
        String uid1 = manifest.jsonObject.opt("uid1").toString();
        String uid2 = manifest.jsonObject.opt("uid2").toString();
        String neirong = manifest.jsonObject.opt("neirong").toString();
        String img = manifest.jsonObject.opt("img").toString();
        String sql = String.format("""
                    SELECT code FROM %s where (uid1 = %s and uid2 = %s) or (uid1= %s and uid2= %s) limit 1
                """, biao_chat, uid1, uid2, uid2, uid1);
        int num = manifest.chaxun_num(sql);
        String code = "";
        if (num > 0) {
            code = gongju.ogetstr(manifest.chaxun_one_2(sql), "code");
        } else {
            code = Long.toString(System.currentTimeMillis());
        }
        String sql2 = String.format("""
                    INSERT into chat (uid1,uid2,neirong,code,img) VALUES ('%s','%s','%s','%s','%s')
                """, uid1, uid2, neirong, code, img);
        manifest.caozuo(sql2);
        String sql3 = String.format("""
                    SELECT id FROM %s where code = '%s' order by id desc limit 1
                """, biao_chat, code);
        String id = gongju.ogetstr(manifest.chaxun_one_2(sql3), "id");
        JSONObject json = new JSONObject();
        json.put("code", 200);
        json.put("data", id);
        return json.toString();
    }

    static String chat_getnew() {
        String code = manifest.jsonObject.opt("code").toString();
        String skip = manifest.jsonObject.opt("skip").toString();
        String sql = String.format(
                """
                            SELECT u.name,u.wxhead,c.neirong,c.addtime,c.id,c.neirong,c.img,c.uid FROM %s u, %s c where c.uid1 = u.id and c.code = '%s' and c.id >  %s
                        """,
                biao_user, biao_chat, code, skip);
        return manifest.chaxun(sql);
    }

    static String chat_getname() {
        String id = manifest.jsonObject.opt("id").toString();
        String sql = "SELECT name FROM " + biao_user + " where id = " + id;
        String name = gongju.ogetstr(manifest.chaxun_one_2(sql), "name");
        JSONObject json = new JSONObject();
        json.put("code", 200);
        json.put("name", name);
        return json.toString();
    }
}
